import Taro from '@tarojs/taro';

import {
  isNumber,
  isBoolean,
  isNil,
  isString,
  isDate,
  isNaN
} from 'lodash';
import _isEmpty from 'lodash/isEmpty';
import config from '../constants/config';


let device;
const designWidth = config.designWidth;

function getDevice() {
  if (_isEmpty(device)) {
    device = Taro.getSystemInfoSync();
  }
  return device;
}

function getDeviceWidth() {
  const res = getDevice();
  return res.windowWidth;
}

function getDeviceHeight() {
  const res = getDevice();
  return res.windowHeight;
}

const getStatusBarHeight = () => getDevice().statusBarHeight.toString() === '' ? 0 : getDevice().statusBarHeight;

const rpxToPx = (rpx) => {
  const pixelRatio1 = designWidth / getDeviceWidth()
  return rpx / pixelRatio1
}
const pxToRpx = (px) => {
  const pixelRatio1 = designWidth / getDeviceWidth()
  return px * pixelRatio1
}
const pxTransform = (px) => Taro.pxTransform(px, designWidth)


export function transToDate(value) {
  if (isDate(value) || _isEmpty(value)) {
    return value;
  }
  let temp = value;
  if (isString(value)) {
    temp = value.replace(/-/g, '/');
  }
  const dateValue = new Date(temp);

  const ifDateType = dateValue instanceof Date && !isNaN(dateValue.getTime());
  return ifDateType ? dateValue : null;
}

function dateFormat(dateTime, fmt) {
  let time = transToDate(dateTime);
  if (!isDate(time)) {
    return dateTime;
  }
  const values = {
    'M+': time.getMonth() + 1, //月份
    'd+': time.getDate(), //日
    'D+': time.getDate(), //日
    'H+': time.getHours(), //小时
    'h+': time.getHours() % 12, //小时
    'm+': time.getMinutes(), //分
    's+': time.getSeconds(), //秒
    'q+': Math.floor((time.getMonth() + 3) / 3), //季度
    S: time.getMilliseconds(), //毫秒
  };
  if (/([y,Y]+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (time.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (const key in values) {
    if (new RegExp('(' + key + ')').test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? values[key] : ('00' + values[key]).substr(('' + values[key]).length)
      );
    }
  }
  return fmt;
}
function formatTime(time, fmt = 'yyyy-MM-dd') {
  if (time) {
    return dateFormat(new Date(time), fmt);
  }
  return '';
}

function formatMoney(money) {
  if (_isEmpty(money)) {
    return '';
  }
  if (isNumber(money)) {
    return money.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
  }
  return money;
}

function isH5() {
  return Taro.getEnv() === Taro.ENV_TYPE.WEB;
}

function isWeapp() {
  return Taro.getEnv() === Taro.ENV_TYPE.WEAPP;
}

function isAlipay() {
    return Taro.getEnv() === Taro.ENV_TYPE.ALIPAY;
}

function isRN() {
    return Taro.getEnv() === Taro.ENV_TYPE.RN;
}

function isJD() {
    return Taro.getEnv() === Taro.ENV_TYPE.JD;
}

function isQQ() {
    return Taro.getEnv() === Taro.ENV_TYPE.QQ;
}

function isByteDance() {
    return Taro.getEnv() === Taro.ENV_TYPE.TT;
}
async function showToast(content) {
 try {
   await Taro.showToast({
     title: content,
     duration:3000,
     success: ()=>{},
     fail: ()=>{}
   })
 }catch (e) {}
 return Promise.resolve();
}

function isEmpty(val) {
  if (isNumber(val) || isBoolean(val)) {
    return false;
  }
  if (isNil(val)) {
    return true;
  }
  if (isString(val)) {
    return val.length === 0;
  }
  return _isEmpty(val);
}

function toBoolean(val) {
  if (_isEmpty(val)) {
    return false;
  }
  if (isString(val)) {
    const p = val.toLowerCase().trim();
    if (p === 'true') {
      return true;
    }
    if (p === 'false') {
      return false;
    }
  }
  return val;
}

function trimProtocal(url = '') {
  const str = url.trim();
  const idx = str.indexOf('page://');
  if (idx > -1) {
    return str.slice(idx + 7);
  }
  return str;
}

export {
    designWidth,
    rpxToPx,
    pxToRpx,
    pxTransform,
    getDevice,
    getStatusBarHeight,
    getDeviceHeight,
    getDeviceWidth,
    formatTime,
    formatMoney,
    isWeapp,
    isAlipay,
    isJD,
    isQQ,
    isByteDance,
    isRN,
    isH5,
    isEmpty,
    toBoolean,
    trimProtocal,
  showToast
}
